package com.example.spinning.controller;

import com.example.spinning.bean.*;
import com.example.spinning.service.*;
import com.example.spinning.utils.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventoryManagement")
public class InventoryManagementController {

    @Autowired
    private EndProductStockService endProductStockService;

    @Autowired
    private EndProductDeliverOrderService endProductDeliverOrderService;

    @Autowired
    private MaterialInputStockService materialInputStockService;

    @Autowired
    private EndProductOutStockService endProductOutStockService;

    @Autowired
    private MaterialOutStockService materialOutStockService;

    @Autowired
    private WareHousePackService wareHousePackService;

    private Logger logger = LoggerFactory.getLogger(getClass());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    //得到所有的打包信息
    @Log(module = "打包管理",description = "得到所有打包信息")
    @RequestMapping(path = "/allWareHousePack",method = RequestMethod.GET)
    public Map<String,Object> getAllWareHousePack(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = wareHousePackService.getWareHousePackCount();
        List<WareHousePack> wareHousePack = wareHousePackService.getAllWareHousePack(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("wareHousePack",wareHousePack);
        return map;
    }

    //得到所有的产品库存
    @Log(module = "成品库存管理",description = "得到所有成品库存")
    @RequestMapping(path = "/allProduct",method = RequestMethod.GET)
    public Map<String,Object> getAllEndProductStocks(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = endProductStockService.getEndProductCount();
        List<endProductStock> productStocks = endProductStockService.getAllEndProductStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("productStocks",productStocks);
        return map;
    }

    //得到所有成品出货单信息
    @Log(module = "成品出货单管理",description = "得到所有成品出货单信息")
    @RequestMapping(path = "/allDeliverOrder",method = RequestMethod.GET)
    public Map<String, Object> getDeliverOrder(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = endProductDeliverOrderService.getDeliverOrderCount();
        List<EndProductDeliverOrder> orders = endProductDeliverOrderService.getAllDeliverOrder(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("orders",orders);
        return map;
    }


    //得到所有原料入库单信息
    @Log(module = "原料入库单管理",description = "得到所有原料入库单信息")
    @RequestMapping(path = "/allInputStock",method = RequestMethod.GET)
    public Map<String, Object> getAllInputOrder(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = materialInputStockService.getInputStockCount();
        List<MaterialInputStock> materialInputStocks = materialInputStockService.getAllInputStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("inputStocks", materialInputStocks);
        return map;
    }

    //得到所有原料出库单信息
    @Log(module = "原料出库单管理",description = "得到所有原料出库单信息")
        @RequestMapping(path = "/allMaterialOutStock",method = RequestMethod.GET)
    public Map<String, Object> allMaterialOutStock(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = materialOutStockService.getMaterialOutStockCount();
        List<MaterialOutStock> materialOutStocks = materialOutStockService.getAllMaterialOutStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("materialOutStocks", materialOutStocks);
        return map;
    }

    //得到所有的出库单信息
    @Log(module = "成品出库单管理",description = "得到所有成品出库单信息")
    @RequestMapping(path = "/allOutStock",method = RequestMethod.GET)
    public Map<String, Object> getAllOutStock(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count
    ){
        int totalCount = endProductOutStockService.getOutStockCount();
        List<EndProductOutStock> endProductOutStocks = endProductOutStockService.getAllOutStock(page,count);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("outStocks", endProductOutStocks);
        return map;
    }

    //通过id查找打包
    @Log(module = "打包管理",description = "根据id查询打包信息")
    @RequestMapping(path = "/getWareHousePackById/",method = RequestMethod.GET)
    public WareHousePack getWareHousePackById(Long id){
        return wareHousePackService.getWareHousePackById(id);
    }

    //通过id查找库存
    @Log(module = "成品库存管理",description = "根据id查询成品库存信息")
    @RequestMapping(path = "/get/",method = RequestMethod.GET)
    public endProductStock getEndProductStockById(Long id){
        return endProductStockService.getEndProductStockById(id);
    }

    //通过id查找出货单
    @Log(module = "成出货单管理",description = "根据id查询成品出货信息")
    @RequestMapping(path = "/getDeliverOrder/",method = RequestMethod.GET)
    public EndProductDeliverOrder getDeliverOrderByIds(Long id){
        return endProductDeliverOrderService.getDeliverOrderById(id);
    }

    //通过id查找原料入库单
    @Log(module = "原料入库单管理",description = "根据id查询原料入库单信息")
    @RequestMapping(path = "/getInputStock/",method = RequestMethod.GET)
    public MaterialInputStock getInputStockByIds(Long id){
        return materialInputStockService.getInputStockById(id);
    }

    //通过id查找原料出库单
    @Log(module = "原料出库单管理",description = "根据id查询原料出库信息")
    @RequestMapping(path = "/getMaterialOutStock/",method = RequestMethod.GET)
    public MaterialOutStock getMaterialOutStock(Long id){
        return materialOutStockService.getMaterialOutStockById(id);
    }

    //通过id查找出库单
    @Log(module = "成品出库单管理",description = "根据id查询成品出库信息")
    @RequestMapping(path = "/getOutStock/",method = RequestMethod.GET)
    public EndProductOutStock getOutStockByIds(Long id){
        return endProductOutStockService.getOutStockById(id);
    }

    //通过条件模糊查找打包
    @Log(module = "打包表管理",description = "模糊查询打包信息")
    @RequestMapping(path = "/findPackByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> findPackByConditionLike(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String workshop,
            String customer,
            String date,
            String batch_no,
            String name

    ){
        int totalCount = wareHousePackService.getWareHousePackCountByCondition(workshop,customer,date,batch_no,name);
        List<WareHousePack> wareHousePacks = wareHousePackService.getWareHousePackByCondition(page,count,workshop,customer,date,batch_no,name);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("wareHousePacks",wareHousePacks);
        return map;
    }

    //通过条件模糊查找库存
    @Log(module = "成品库存表管理",description = "根据条件模糊查找库存")
    @RequestMapping(path = "/findEndByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getEndProductStockByCondition(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String name,
            String batch_no,
            String norms,
            String date
    ){
        int totalCount = endProductStockService.getEndProductCountByCondition(name,batch_no,norms,date);
        List<endProductStock> productStocks = endProductStockService.getEndProduceStockByCondition(page,count,name,batch_no,norms,date);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("productStocks",productStocks);
        return map;
    }

    //根据条件模糊查找出货单
    @Log(module = "成品出货单管理",description = "添加成品出货信息")
    @RequestMapping(path = "/findDeliverByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getDeliverOrderByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String date,
            String receiver,
            String norms,
            String batch_no
    ){
        int totalCount = endProductDeliverOrderService.getDeliverOrderCountByCondition(date,receiver,norms,batch_no);
        List<EndProductDeliverOrder> endProductDeliverOrders = endProductDeliverOrderService.getDeliverOrderByCondition(page,count,date,receiver,norms,batch_no);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("deliverOrders", endProductDeliverOrders);
        return map;
    }

    //根据条件模糊查找入库单
    @Log(module = "原料入库单管理",description = "添加原料入库信息")
    @RequestMapping(path = "/findInputByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getMaterialInputStockByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String unit,
            String date,
            String name,
            String prupose
    ){
        int totalCount = materialInputStockService.getInputStockCountByCondition(unit,date,name,prupose);
        List<MaterialInputStock> materialInputStocks = materialInputStockService.getInputStockByCondition(page,count,unit,date,name,prupose);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("inputStocks", materialInputStocks);
        return map;
    }

    //根据条件模糊查找出库单
    @Log(module = "原料出库单管理",description = "添加原料出库信息")
    @RequestMapping(path = "/findMaterialOutByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> findMaterialOutByConditionLike(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String unit,
            String date,
            String name,
            String prupose
    ){
        int totalCount = materialOutStockService.getMaterialOutStockCountByCondition(unit,date,name,prupose);
        List<MaterialOutStock> materialOutStocks = materialOutStockService.getMaterialOutStockByCondition(page,count,unit,date,name,prupose);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("inputStocks", materialOutStocks);
        return map;
    }

    //根据条件模糊查找出库单
    @Log(module = "成品出库单管理",description = "添加成品出库信息")
    @RequestMapping(path = "/findOutByConditionLike/",method = RequestMethod.GET)
    public Map<String,Object> getOutStockByName(
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "count") Integer count,
            String date,
            String receiver,
            String norms,
            String batch_no
    ){
        int totalCount = endProductOutStockService.getOutStockCountByCondition(date,receiver,norms,batch_no);
        List<EndProductOutStock> endProductOutStocks = endProductOutStockService.getOutStockByCondition(page,count,date,receiver,norms,batch_no);
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("outStocks", endProductOutStocks);
        return map;
    }

    //添加打包
    @Log(module = "打包管理",description = "添加打包记录")
    @RequestMapping(path = "/addWareHouse/",method = RequestMethod.POST)
    public RespBean addWareHouse(WareHousePack wareHousePack) {
//        if ("".equals(wareHousePack.getName())|| wareHousePack.getName() == null) {
//            return new RespBean("error", "品名不能为空!");
//        }
//        if ("".equals(wareHousePack.getBatch_no())|| wareHousePack.getBatch_no() == null) {
//            return new RespBean("error", "批号不能为空!");
//        }
        int result = wareHousePackService.addWareHousePack(wareHousePack);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加成品库存
    @Log(module = "成品库存管理",description = "添加成品库存")
    @RequestMapping(path = "/addEndProduct/",method = RequestMethod.POST)
    public RespBean addEndProduct(endProductStock productStock) {
        if ("".equals(productStock.getName())|| productStock.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(productStock.getBatch_no())|| productStock.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = endProductStockService.addEndProductStock(productStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加出货单信息
    @Log(module = "成品出货单管理",description = "添加成品出货单信息")
    @RequestMapping(path = "/addDeliverOrder/",method = RequestMethod.POST)
    public RespBean addDeliverOrder(EndProductDeliverOrder endProductDeliverOrder) {

        int result = endProductDeliverOrderService.addDeliverOrder(endProductDeliverOrder);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加入库单信息
    @Log(module = "原料入库单管理",description = "添加原料入库单信息")
    @RequestMapping(path = "/addInputStock/",method = RequestMethod.POST)
    public RespBean addInputStock(MaterialInputStock materialInputStock) {

        int result = materialInputStockService.addInputStock(materialInputStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加原料出库单信息
    @Log(module = "原料出库单管理",description = "添加原料出库单信息")
    @RequestMapping(path = "/addMaterialOutStock/",method = RequestMethod.POST)
    public RespBean addMaterialOutStock(MaterialOutStock materialOutStock) {

        int result = materialOutStockService.addMaterialOutStock(materialOutStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }

    //添加出库单信息
    @Log(module = "成品出库单管理",description = "添加成品出库单信息")
    @RequestMapping(path = "/addOutStock/",method = RequestMethod.POST)
    public RespBean addOutStock(EndProductOutStock endProductOutStock) {

        int result = endProductOutStockService.addOutStock(endProductOutStock);

        if (result == 1) {
            return new RespBean("success", "添加成功!");
        }
        return new RespBean("error", "添加失败!");
    }


    //修改打包
    @Log(module = "打包管理",description = "修改打包信息")
    @RequestMapping(path = "/updateWareHousePack/",method = RequestMethod.PUT)
    public RespBean updateWareHousePack(WareHousePack wareHousePack){
        int i = wareHousePackService.updateWareHousePack(wareHousePack);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改产品库存
    @Log(module = "成品库存管理",description = "修改成品库存")
    @RequestMapping(path = "/updateEnd/",method = RequestMethod.PUT)
    public RespBean updateEndProStock(endProductStock productStock){
        if ("".equals(productStock.getName())|| productStock.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(productStock.getBatch_no())|| productStock.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        //修改行数
        int i = endProductStockService.updateEndProductStockById(productStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return  new RespBean("error","修改失败！");
    }

    //修改出货单信息
    @Log(module = "成品出货单管理",description = "修改成品出货单信息")
    @RequestMapping(path = "/updateDeliver/",method = RequestMethod.PUT)
    public RespBean updateDeliverOrder(EndProductDeliverOrder endProductDeliverOrder){
        int i = endProductDeliverOrderService.updateDeliverOrder(endProductDeliverOrder);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改入库单信息
    @Log(module = "原料入库单管理",description = "修改原料入库单信息")
    @RequestMapping(path = "/updateInputStock/",method = RequestMethod.PUT)
    public RespBean updateInputStock(MaterialInputStock materialInputStock){
        int i = materialInputStockService.updateInputStock(materialInputStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改出库单信息
    @Log(module = "原料出库单管理",description = "修改原料出库单信息")
    @RequestMapping(path = "/updateMaterialOutStock/",method = RequestMethod.PUT)
    public RespBean updateMaterialOutStock(MaterialOutStock materialOutStock){
        int i = materialOutStockService.updateMaterialOutStock(materialOutStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }

    //修改出库单信息
    @Log(module = "成品出库单管理",description = "修改成品出库单信息")
    @RequestMapping(path = "/updateOutStock/",method = RequestMethod.PUT)
    public RespBean updateOutStock(EndProductOutStock endProductOutStock){
        int i = endProductOutStockService.updateOutStock(endProductOutStock);
        if(i == 1){
            return new RespBean("success","修改成功！");
        }
        return new RespBean("error","修改失败！");
    }


    //删除打包
    @Log(module = "打包管理",description = "删除打包信息")
    @RequestMapping(value = "/deleteWareHousePackById/",method = RequestMethod.DELETE)
    public RespBean deleteWareHousePackById(String ids){
        boolean result = wareHousePackService.deleteWareHousePackByIds(ids);
        if(result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除库存产品
    @Log(module = "成品库存管理",description = "删除成品库存信息")
    @RequestMapping(value = "/del/",method = RequestMethod.DELETE)
    public RespBean deleteEndProductStockById(String ids){
        boolean result = endProductStockService.deleteEndProductStockByIds(ids);
        if(result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除出货单
    @Log(module = "成品出货单管理",description = "删除成品出货单信息")
    @RequestMapping(path = "/delDeliver/",method = RequestMethod.DELETE)
    public RespBean deleteDeliverById( String ids){
        boolean result = endProductDeliverOrderService.deleteDeliverOrderByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除入库单
    @Log(module = "原料入库单管理",description = "删除原料入库单信息")
    @RequestMapping(path = "/delInputStock/",method = RequestMethod.DELETE)
    public RespBean deleteInputStockById(String ids){
        boolean result = materialInputStockService.deleteInputStockByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除出库单
    @Log(module = "原料出库单管理",description = "删除原料出库单信息")
    @RequestMapping(path = "/delMaterialOutStock/",method = RequestMethod.DELETE)
    public RespBean delMaterialOutStock(String ids){
        boolean result = materialOutStockService.deleteMaterialOutStockByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

    //删除出库单
    @Log(module = "成品出库单管理",description = "删除成品出库单信息")
    @RequestMapping(path = "/delOutStock/",method = RequestMethod.DELETE)
    public RespBean deleteOutStockById(String ids){
        boolean result = endProductOutStockService.deleteOutStockByIds(ids);
        if (result){
            return new RespBean("success","删除成功！");
        }
        return new RespBean("error","删除失败！");
    }

}
